var app = getApp()
Page({

  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio')
  },

  data: {
    categoryId: 4,
    hideHeader: true,
    hideBottom: true,
    // refreshTime: '', // 刷新的时间 
    contentlist: null, // 列表显示的数据源
    controls: true,
    allPages: null, // 总页数
    currentPage: 1, // 当前页数  默认是1
    loadMoreData:"加载更多...."
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var date = new Date();
    // this.setData({
    //   refreshTime: date.toLocaleTimeString()
    // })
    this.getData();
  },
  // 上拉加载更多
  loadMore: function() {
    var self = this;
    // 当前页是最后一页
    if (self.data.currentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '我也是有底线的'
      })
      return;
    }
    setTimeout(function() {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.currentPage;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        currentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getData();
      self.setData({
        hideBottom: true
      })
    }, 300);
  },
  // 下拉刷新
  refresh: function(e) {
    var self = this;
    setTimeout(function() {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        currentPage: 1,
        hideHeader: false
      })
      self.getData();
      // self.setData({
      //   hideHeader: true
      // })
    }, 300);
  },
  // 获取数据  pageIndex：页码参数
  getData: function() {
    var self = this;
    var pageIndex = self.data.currentPage;
    var categoryId = self.data.categoryId;
    wx.request({
      url: 'http://xxxxxxxx:8080/webchat/list/querybroacastlist',
      data: {
        schoolId: app.globalData.userInfo.schoolid,
        categoryId: categoryId,
        pageIndex: pageIndex
      },
      success: function(res) {

        console.log(res.data)
        if (pageIndex == 1) { // 下拉刷新
          self.setData({
            allPages: res.data.data.allPages,
            contentlist: res.data.data.message,
            hideHeader: true
          })
        } else { // 加载更多
          console.log('加载更多');
          var tempArray = self.data.contentlist;
          tempArray = tempArray.concat(res.data.data.message);
          self.setData({
            allPages: res.data.data.allPages,
            contentlist: tempArray,
            hideBottom: true
          })


        }
      },
      fail: function() {

      }
    })
  },
 
  audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  },
  audio14: function () {
    this.audioCtx.seek(14)
  },
  audioStart: function () {
    this.audioCtx.seek(0)
  }
})